package com.yanqu.road.server.gameplayer.module.activity.tomb.cmd;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.tomb.Tomb;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.tomb.TombMgr;
import com.yanqu.road.server.gameplayer.module.activity.tomb.TombModule;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.sync.message.SyncResponse;


import java.util.concurrent.CompletableFuture;

@Cmd(code = GameProtocol.S_TOMB_CHANGE_DOLL,desc = "秦始皇陵更换人偶")
public class TombChangeDollCmd extends AbstractCommand {

    private Tomb.TombChangeDollReqMsg reqMsg;

    private Tomb.TombChangeDollRespMsg.Builder respMsg;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        this.reqMsg = Tomb.TombChangeDollReqMsg.parseFrom(packet.getBytes());
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        this.respMsg = Tomb.TombChangeDollRespMsg.newBuilder();
        ActivityInfo activityInfo = TombMgr.getActivityInfo();
        if (activityInfo == null) {
            this.respMsg.setRet(GameErrorCode.E_ACTIVITY_NOT_OPEN);
            return;
        }
        int ret = player.getModule(TombModule.class).isOpen(true);
        if (ret > 0) {
            this.respMsg.setRet(ret);
            player.sendPacket(ClientProtocol.U_TOMB_CHANGE_DOLL, this.respMsg);
            return;
        }
        if (!TombMgr.getConfig().getConfig().getTOMB_ALL_DOLL_ID_LIST().contains(this.reqMsg.getDollId())){
            this.respMsg.setRet(GameErrorCode.E_TOMB_DOLL_NO_FOUND);
            player.sendPacket(ClientProtocol.U_TOMB_CHANGE_DOLL, this.respMsg);
            return;
        }
        Tomb.TombChangeDollReqMsg.Builder reqBuilder =reqMsg.toBuilder();
        reqBuilder.setActivityId(activityInfo.getActivityId());
        CompletableFuture<SyncResponse> future = GamePlayerMgr.sendToActivityCrossServerForUnionGroup(player.getUserId(), CrossProtocol.C_TOMB_CHANGE_DOLL,activityInfo.getActivityId(),reqBuilder,this.respMsg);
        future.thenAccept(syncResponse -> {

            if (!this.respMsg.hasRet()) {
                this.respMsg.setRet(0);
            }
            player.sendPacket(ClientProtocol.U_TOMB_CHANGE_DOLL, this.respMsg);

        });




    }

    @Override
    public void sendData(GamePlayer player) {

    }
}
